﻿using System.Threading;
using CodeCamp.BL;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace CodeCamp.Tests
{


	[TestClass()]
	public class SyndicationBLTest
	{

		[TestMethod]
		public void RefreshAsync_TwitterUrl_FiresRefreshedEventWithin30sec()
		{
			var waitHandle = new ManualResetEvent(false);
			var target = new SyndicationBL();
			target.SyndicationUrl = "http://search.twitter.com/search.atom?q=%23CodeCamp";
			target.Updated += (o, e) =>
				waitHandle.Set();
			target.RefreshAsync();
			Assert.IsTrue(waitHandle.WaitOne(30000));
		}

		[TestMethod]
		public void Refresh_TwitterUrl_ReturnsItems()
		{
			var syndicationUrl = "http://search.twitter.com/search.atom?q=%23CodeCamp";
			var actual = SyndicationBL.Refresh(syndicationUrl);
			Assert.IsNotNull(actual);
			Assert.IsTrue(actual.Count > 0);

		}

		/// <summary>
		///A test for notify
		///</summary>
		[TestMethod()]
		[DeploymentItem("CodeCamp.BL.dll")]
		public void notify_subscribed_notifies()
		{
			var waitHandle = new ManualResetEvent(false);
			SyndicationBL_Accessor target = new SyndicationBL_Accessor();
			target.add_Updated((o, e) => waitHandle.Set());
			target.notify();
			Assert.IsTrue(waitHandle.WaitOne(10));
		}

		[TestMethod()]
		[DeploymentItem("CodeCamp.BL.dll")]
		public void notify_nothingSubscribed_DoesNotNotify()
		{
			var waitHandle = new ManualResetEvent(false);
			SyndicationBL_Accessor target = new SyndicationBL_Accessor();
			target.notify();
			Assert.IsFalse(waitHandle.WaitOne(10));
		}
	}
}
